package com.cloudweb.oa.controller;


import cn.js.fan.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloudweb.oa.entity.Role;
import com.cloudweb.oa.entity.UserRoleKind;
import com.cloudweb.oa.security.AuthUtil;
import com.cloudweb.oa.service.IUserRoleKindService;
import com.cloudweb.oa.vo.Result;
import com.redmoon.oa.basic.SelectDb;
import com.redmoon.oa.basic.SelectOptionDb;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fgf
 * @since 2025-01-30
 */
@Api(tags = "角色的类型")
@RestController
@RequestMapping("/admin/role")
public class UserRoleKindController {

    @Autowired
    IUserRoleKindService userRoleKindService;

    @Autowired
    AuthUtil authUtil;

    @ApiOperation(value = "创建角色类型", notes = "创建角色类型", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = true, dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @RequestMapping(value = "/createRoleKind", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    @ResponseBody
    public Result<JSONObject> createRoleKind(@RequestParam String name) {
        int order = userRoleKindService.getMaxOrder(authUtil.getUserUnitCode());
        UserRoleKind userRoleKind = new UserRoleKind();
        userRoleKind.setName(name);
        userRoleKind.setOrders(order + 1);
        userRoleKind.setUnitCode(authUtil.getUserUnitCode());
        return new Result<>(userRoleKind.insert());
    }

    @ApiOperation(value = "删除角色类型", notes = "删除角色类型", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "角色类型的值", required = true, dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @RequestMapping(value = "/delRoleKind", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    @ResponseBody
    public Result<JSONObject> delRoleKind(@RequestParam(defaultValue = "") String code) {
        long id = StrUtil.toLong(code);
        return new Result<>(userRoleKindService.removeById(id));
    }

    @ApiOperation(value = "修改角色类型", notes = "修改角色类型", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "角色类型的值", required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "名称", required = true, dataType = "String"),
    })
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @RequestMapping(value = "/updateRoleKind", method = RequestMethod.POST, produces = {"text/html;charset=UTF-8;", "application/json;charset=UTF-8;"})
    @ResponseBody
    public Result<JSONObject> updateRoleKind(@RequestParam String code, @RequestParam String name) {
        long id = StrUtil.toLong(code);
        UserRoleKind userRoleKind = userRoleKindService.getById(id);
        userRoleKind.setName(name);
        QueryWrapper<UserRoleKind> qw = new QueryWrapper<>();
        qw.eq("id", id);
        return new Result<>(userRoleKindService.update(userRoleKind, qw));
    }

    @ApiOperation(value = "取得角色树", notes = "取得角色树", httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @ResponseBody
    @RequestMapping(value = "/listRoleKind", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8;"})
    public Result<Object> listRoleKind() {
        List<UserRoleKind> list = userRoleKindService.listByUnitCode(authUtil.getUserUnitCode());
        return new Result<>(list);
    }
}

